package Visibility;

import java.util.HashMap;

public class FinalFieldExample {
    final int x;
    HashMap<Integer, Integer> map;
    int y;
    static FinalFieldExample f;

    public FinalFieldExample() {
        x = 3;
        y = 4;
    }

    static void writer() {
        f = new FinalFieldExample();
    }

    static void reader() {
        if (f != null) {
            System.out.println(f.x);  // guaranteed to see 3
            System.out.println(f.y);// could see 0
        }
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 1000; i++) {
        Thread write = new Thread() {
            public void run() {
                writer();
            }
        };
        Thread read = new Thread() {
            @Override
            public void run() {
                reader();
            }
        };
//        Thread read2 = new Thread() {
//            @Override
//            public void run() {
//                reader();
//            }
//        };
        write.start();

        read.start();
    }
//    }
}
